/*
 * Copyright:  2018 smarabbit studio.
 *
 *  Licensed under the Confluent Community License; you may not use this file
 *  except in compliance with the License.  You may obtain a copy of the License at
 *
 *  http://www.confluent.io/confluent-community-license
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 *  WARRANTIES OF ANY KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations under the License.
 *
 *  @作   者： 黄开晖 (117227773@qq.com)
 *  @日   期:  2020年9月24日
 */
package com.massyframework.beanskin.runtime.framework.installing.strategy;

import java.util.Objects;

import com.massyframework.beanskin.service.Filter;
import com.massyframework.beanskin.service.ServiceRepository;
import com.massyframework.beanskin.service.awaitstrategy.AwaitServiceDescriptor;

/**
 * 等待服务描述
 * @author huangkh
 *
 */
public class DefaultAwaitServiceDescriptor<S> implements AwaitServiceDescriptor<S>{
	
	private final Class<S> requiredType;
	private final String filterString;
	private final Filter filter;

	/**
	 * 构造方法
	 * @param builder {@link Builder}
	 */
	protected DefaultAwaitServiceDescriptor(Builder<S> builder) {
		this.requiredType= Objects.requireNonNull(builder.requiredType, "\"requiredType\" cannot be null.");
		this.filterString = builder.filterString;
		this.filter = builder.serviceRepository.createFilter(this.filterString);
	}
	
	/**
	 * 依赖的类型
	 * @return {@link Class}
	 */
	public Class<S> getRequiredType(){
		return this.requiredType;
	}
	
	/**
	 * 过滤字符串
	 * @return {@link String}
	 */
	public String getFilterString() {
		return this.filterString;
	}
	
	
	
	/**
	 * 过滤条件
	 * @return {@link Filter}
	 */
	public Filter getFilter() {
		return this.filter;
	}
	
	/**
	 * 创建构建器
	 * @return {@link Builder}
	 */
	public static <S> Builder<S> newBuilder() {
		return new Builder<S>();
	}

	/**
	 * 构建器
	 * @author huangkh
	 */
	public static class Builder<S> {
		private Class<S> requiredType;
		private String filterString;
		private ServiceRepository serviceRepository;
		
		/**
		 * 设置依赖的类型
		 * @param value {@link Class}
		 * @return {@link Builder}
		 */
		public Builder<S> requiredType(Class<S> value) {
			this.requiredType = value;
			return this;
		}
		
		/**
		 * 设置过滤条件
		 * @param value {@link String}
		 * @return
		 */
		public Builder<S> filterString(String value) {
			this.filterString = value;
			return this;
		}
		
		/**
		 * 设置服务仓储
		 * @param value {@link ServiceRepository}
		 * @return {@link Builder}
		 */
		public Builder<S> serviceRepository(ServiceRepository value) {
			this.serviceRepository = value;
			return this;
		}
		
		/**
		 * 构建依赖服务记录 
		 * @return {@link DefaultAwaitServiceDescriptor}
		 */
		public DefaultAwaitServiceDescriptor<S> build() {
			return new DefaultAwaitServiceDescriptor<S>(this);
		}
		
	}
}
